/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLEUR;

import CONTROLEUR.exceptions.IllegalOrphanException;
import CONTROLEUR.exceptions.NonexistentEntityException;
import CONTROLEUR.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import MODEL.Acheter;
import MODEL.Client;
import java.util.ArrayList;
import java.util.Collection;
import MODEL.Reserver2;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author ahmed
 */
public class ClientJpaController implements Serializable {

    public ClientJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Client client) throws PreexistingEntityException, Exception {
        if (client.getAcheterCollection() == null) {
            client.setAcheterCollection(new ArrayList<Acheter>());
        }
        if (client.getReserver2Collection() == null) {
            client.setReserver2Collection(new ArrayList<Reserver2>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Acheter> attachedAcheterCollection = new ArrayList<Acheter>();
            for (Acheter acheterCollectionAcheterToAttach : client.getAcheterCollection()) {
                acheterCollectionAcheterToAttach = em.getReference(acheterCollectionAcheterToAttach.getClass(), acheterCollectionAcheterToAttach.getAcheterPK());
                attachedAcheterCollection.add(acheterCollectionAcheterToAttach);
            }
            client.setAcheterCollection(attachedAcheterCollection);
            Collection<Reserver2> attachedReserver2Collection = new ArrayList<Reserver2>();
            for (Reserver2 reserver2CollectionReserver2ToAttach : client.getReserver2Collection()) {
                reserver2CollectionReserver2ToAttach = em.getReference(reserver2CollectionReserver2ToAttach.getClass(), reserver2CollectionReserver2ToAttach.getReserver2PK());
                attachedReserver2Collection.add(reserver2CollectionReserver2ToAttach);
            }
            client.setReserver2Collection(attachedReserver2Collection);
            em.persist(client);
            for (Acheter acheterCollectionAcheter : client.getAcheterCollection()) {
                Client oldClientOfAcheterCollectionAcheter = acheterCollectionAcheter.getClient();
                acheterCollectionAcheter.setClient(client);
                acheterCollectionAcheter = em.merge(acheterCollectionAcheter);
                if (oldClientOfAcheterCollectionAcheter != null) {
                    oldClientOfAcheterCollectionAcheter.getAcheterCollection().remove(acheterCollectionAcheter);
                    oldClientOfAcheterCollectionAcheter = em.merge(oldClientOfAcheterCollectionAcheter);
                }
            }
            for (Reserver2 reserver2CollectionReserver2 : client.getReserver2Collection()) {
                Client oldClientOfReserver2CollectionReserver2 = reserver2CollectionReserver2.getClient();
                reserver2CollectionReserver2.setClient(client);
                reserver2CollectionReserver2 = em.merge(reserver2CollectionReserver2);
                if (oldClientOfReserver2CollectionReserver2 != null) {
                    oldClientOfReserver2CollectionReserver2.getReserver2Collection().remove(reserver2CollectionReserver2);
                    oldClientOfReserver2CollectionReserver2 = em.merge(oldClientOfReserver2CollectionReserver2);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findClient(client.getIdC()) != null) {
                throw new PreexistingEntityException("Client " + client + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Client client) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Client persistentClient = em.find(Client.class, client.getIdC());
            Collection<Acheter> acheterCollectionOld = persistentClient.getAcheterCollection();
            Collection<Acheter> acheterCollectionNew = client.getAcheterCollection();
            Collection<Reserver2> reserver2CollectionOld = persistentClient.getReserver2Collection();
            Collection<Reserver2> reserver2CollectionNew = client.getReserver2Collection();
            List<String> illegalOrphanMessages = null;
            for (Acheter acheterCollectionOldAcheter : acheterCollectionOld) {
                if (!acheterCollectionNew.contains(acheterCollectionOldAcheter)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Acheter " + acheterCollectionOldAcheter + " since its client field is not nullable.");
                }
            }
            for (Reserver2 reserver2CollectionOldReserver2 : reserver2CollectionOld) {
                if (!reserver2CollectionNew.contains(reserver2CollectionOldReserver2)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Reserver2 " + reserver2CollectionOldReserver2 + " since its client field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Acheter> attachedAcheterCollectionNew = new ArrayList<Acheter>();
            for (Acheter acheterCollectionNewAcheterToAttach : acheterCollectionNew) {
                acheterCollectionNewAcheterToAttach = em.getReference(acheterCollectionNewAcheterToAttach.getClass(), acheterCollectionNewAcheterToAttach.getAcheterPK());
                attachedAcheterCollectionNew.add(acheterCollectionNewAcheterToAttach);
            }
            acheterCollectionNew = attachedAcheterCollectionNew;
            client.setAcheterCollection(acheterCollectionNew);
            Collection<Reserver2> attachedReserver2CollectionNew = new ArrayList<Reserver2>();
            for (Reserver2 reserver2CollectionNewReserver2ToAttach : reserver2CollectionNew) {
                reserver2CollectionNewReserver2ToAttach = em.getReference(reserver2CollectionNewReserver2ToAttach.getClass(), reserver2CollectionNewReserver2ToAttach.getReserver2PK());
                attachedReserver2CollectionNew.add(reserver2CollectionNewReserver2ToAttach);
            }
            reserver2CollectionNew = attachedReserver2CollectionNew;
            client.setReserver2Collection(reserver2CollectionNew);
            client = em.merge(client);
            for (Acheter acheterCollectionNewAcheter : acheterCollectionNew) {
                if (!acheterCollectionOld.contains(acheterCollectionNewAcheter)) {
                    Client oldClientOfAcheterCollectionNewAcheter = acheterCollectionNewAcheter.getClient();
                    acheterCollectionNewAcheter.setClient(client);
                    acheterCollectionNewAcheter = em.merge(acheterCollectionNewAcheter);
                    if (oldClientOfAcheterCollectionNewAcheter != null && !oldClientOfAcheterCollectionNewAcheter.equals(client)) {
                        oldClientOfAcheterCollectionNewAcheter.getAcheterCollection().remove(acheterCollectionNewAcheter);
                        oldClientOfAcheterCollectionNewAcheter = em.merge(oldClientOfAcheterCollectionNewAcheter);
                    }
                }
            }
            for (Reserver2 reserver2CollectionNewReserver2 : reserver2CollectionNew) {
                if (!reserver2CollectionOld.contains(reserver2CollectionNewReserver2)) {
                    Client oldClientOfReserver2CollectionNewReserver2 = reserver2CollectionNewReserver2.getClient();
                    reserver2CollectionNewReserver2.setClient(client);
                    reserver2CollectionNewReserver2 = em.merge(reserver2CollectionNewReserver2);
                    if (oldClientOfReserver2CollectionNewReserver2 != null && !oldClientOfReserver2CollectionNewReserver2.equals(client)) {
                        oldClientOfReserver2CollectionNewReserver2.getReserver2Collection().remove(reserver2CollectionNewReserver2);
                        oldClientOfReserver2CollectionNewReserver2 = em.merge(oldClientOfReserver2CollectionNewReserver2);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = client.getIdC();
                if (findClient(id) == null) {
                    throw new NonexistentEntityException("The client with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Client client;
            try {
                client = em.getReference(Client.class, id);
                client.getIdC();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The client with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Acheter> acheterCollectionOrphanCheck = client.getAcheterCollection();
            for (Acheter acheterCollectionOrphanCheckAcheter : acheterCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Client (" + client + ") cannot be destroyed since the Acheter " + acheterCollectionOrphanCheckAcheter + " in its acheterCollection field has a non-nullable client field.");
            }
            Collection<Reserver2> reserver2CollectionOrphanCheck = client.getReserver2Collection();
            for (Reserver2 reserver2CollectionOrphanCheckReserver2 : reserver2CollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Client (" + client + ") cannot be destroyed since the Reserver2 " + reserver2CollectionOrphanCheckReserver2 + " in its reserver2Collection field has a non-nullable client field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(client);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Client> findClientEntities() {
        return findClientEntities(true, -1, -1);
    }

    public List<Client> findClientEntities(int maxResults, int firstResult) {
        return findClientEntities(false, maxResults, firstResult);
    }

    private List<Client> findClientEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Client.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Client findClient(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Client.class, id);
        } finally {
            em.close();
        }
    }

    public int getClientCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Client> rt = cq.from(Client.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
